/**
 * Copyright 2009, OpenScriptures.org
 * GPL 3 License: http://www.gnu.org/licenses/gpl.html
 * $Id: reference-parser.js 152 2009-02-23 05:00:30Z WestonRuter@gmail.com $
 */


var ntBookRegExps = [
	[/^1\s*C\D*/i,		"1Cor"],
	[/^1\s*J\D*/i,		"1John"],
	[/^1\s*P\D*/i,		"1Pet"],
	[/^1\s*Th\D*/i,		"1Thess"],
	[/^1\s*T[im]\D*/i,	"1Tim"],
	[/^2\s*C\D*/i,		"2Cor"],
	[/^2\s*J\D*/i,		"2John"],
	[/^2\s*P\D*/i,		"2Pet"],
	[/^2\s*Th\D*/i,		"2Thess"],
	[/^2\s*T[im]\D*/i,	"2Tim"],
	[/^3\s*J\D*/i,		"3John"],
	[/^A\D*/i,			"Acts"],
	[/^C\D*/i,			"Col"],
	[/^E\D*/i,			"Eph"],
	[/^G\D*/i,			"Gal"],
	[/^H\D*/i,			"Heb"],
	[/^J[mas]\D*/i,		"Jas"],
	[/^Jo?[hn]\D*/i,	"John"],
	[/^Ju\D*/i,			"Jude"],
	[/^L\D*/i,			"Luke"],
	[/^Ma?[rk]\D*/i,	"Mark"],
	[/^Ma?t?t\D*/i,		"Matt"],
	[/^Ph\w*m\D*/i,		"Phlm"],
	//[/^Ph(il|\w*p)\D*/i,"Phil"],
	[/^Ph\D*/i,			"Phil"],
	[/^R[ev]\D*/i,		"Rev"],
	[/^R[om]\D*/i,		"Rom"],
	[/^T\D*/i,			"Titus"]
];

/**
 * Returns an osisRef
 */
function parseReference(ref){
	var parts = ref.replace(/^\s+|\s+$/g, '').replace(/\u2013|\u2014/g, '-').split(/\s*-+\s*/);
	
	var osisref;
	var startBook,
	    startChapter,
		startVerse,
		endBook,
		endChapter,
		endVerse;
	
	//Parse the start ref
	$(ntBookRegExps).each(function(){
		if(this[0].test(parts[0])){
			//Get book
			startBook = this[1];
			parts[0] = parts[0].replace(this[0], '');
			
			//Chapter
			var chapVerseParts = parts[0].match(/(\d+)(?:\D+(\d+)(?:\s*,\s*(\d+))?)?/);
			if(chapVerseParts){
				startChapter = parseInt(chapVerseParts[1], 10);
				
				//Verse
				if(chapVerseParts[2])
					startVerse = parseInt(chapVerseParts[2], 10);
				
				//If a comma is separating two verses then honor that
				if(chapVerseParts[3]){
					endBook = startBook;
					endChapter = startChapter;
					endVerse = parseInt(chapVerseParts[3], 10);
					if(endVerse != startVerse+1)
						endVerse = null;
				}
			}
			
			return false;
		}
		return true;
	});
	if(!startBook)
		return '';
	
	osisref = startBook;
	if(startChapter)
		osisref += '.' + startChapter;
	if(startVerse)
		osisref += '.' + startVerse;
	
	//If reference is range, parse the ending ref
	if(parts[1]){
		$(ntBookRegExps).each(function(){
			if(this[0].test(parts[1])){
				endBook = this[1];
				parts[1] = parts[1].replace(this[0], '');
				return false;
			}
			return true;
		});
			
		//Chapter
		var chapVerseParts = parts[1].match(/(\d+)(?:\D+(\d+))?/);
		if(chapVerseParts){
			
			//If only one number is in ref end, and there is no book specified
			if(!chapVerseParts[2] && !endBook){
				//If verse specified in start ref, then the number is the verse
				if(startVerse){
					endChapter = startChapter;
					endVerse = parseInt(chapVerseParts[1], 10);
				}
				//If only chapter specified, then the number is a chapter
				else if(startChapter){
					endChapter = parseInt(chapVerseParts[1], 10);
				}
			}
			//Either the book is supplied or a chapter and verse is supplied; in this case, don't adopt anything from start ref
			else {
				endChapter = parseInt(chapVerseParts[1], 10);
				if(chapVerseParts[2])
					endVerse = parseInt(chapVerseParts[2], 10);
			}
		}
		
		if(!endBook)
			endBook = startBook;
	}

	if(endBook){
		osisref += '-' + endBook;
		if(endChapter)
			osisref += '.' + endChapter;
		if(endVerse)
			osisref += '.' + endVerse;
	}
	
	//1 John 3:1,2
	//1 John 3:2-1 John 3:3
	//1 John 3-1 John 4
	//1 John 1:2-1 John 4:2
	
	return osisref;
}